import { ref } from "vue";
import { defineStore, storeToRefs } from "pinia";

import { usePersistStore } from "@/stores";
import {
  delTobaccoShortService,
  getTobaccoShortService,
} from "@/api/tobaccoApi";
import { ElMessage } from "element-plus";

export const useShortStore = defineStore("short", () => {
  const persistStore = usePersistStore();
  const { userInfo, shortList } = storeToRefs(persistStore);
  //获取不足的单料烟信息
  const getTobaccoShort = async () => {
    const res = await getTobaccoShortService();
    shortList.value = res.data;
  };
  //缺料信息的删除
  const shortDelete = async (index, row) => {
    const res = await delTobaccoShortService({ rfid: row.rfid });
    getTobaccoShort();
    ElMessage.success("移除成功");
  };
  //缺料信息导出
  //文件导出
  const downLoadData = () => {
    window.open(
      "http://localhost:8080/tobacco/short/download?Authorization=" +
        userInfo.value.token
    );
    shortList.value = [];
  };

  return {
    downLoadData,
    shortDelete,
    getTobaccoShort,
  };
});
